/**
 * Created by maiseries on 2018/1/4.
 */
import React from 'react';
import Picker from 'react-native-picker'

import MaiFormStatus from './form/FormStatus'
import MaiSubmitBtn from './form/SubmitBtn'
import MaiPassportField from './form/PassportField'

import MaiDefaultFieldType from './form/DefaultFieldType'
import MaiDefaultField from './form/DefaultField'
import MaiDefaultInput from './form/DefaultInput'
import MaiDefaultTextArea from './form/DefaultTextArea'
import MaiDefaultLabel from './form/DefaultLabel'
import MaiDefaultUpload from './form/DefaultUpload'

/**
 * 表单状态
 * @type {{EDIT: string, READY: string, SUBMITTING: string}} EDIT: 编辑中, READY: 就绪, SUBMITTING: 正在提交
 */
export const FormStatus = MaiFormStatus;
/**
 * 默认字段行类型
 * @type {{INLINE: string, BLOCK: string}} INLINE：行内单行，BLOCK：块级多行
 */
export const DefaultFieldType = MaiDefaultFieldType;
/**
 * 默认字段行
 * @type {DefaultField}
 */
export const DefaultField = MaiDefaultField;
/**
 * 默认文本框
 * @type {DefaultInput}
 */
export const DefaultInput = MaiDefaultInput;
/**
 * 默认文本域
 * @type {DefaultTextArea}
 */
export const DefaultTextArea = MaiDefaultTextArea;
/**
 * 默认显示框
 * @type {DefaultLabel}
 */
export const DefaultLabel = MaiDefaultLabel;
/**
 * 默认上传组件
 * @type {DefaultUpload}
 */
export const DefaultUpload = MaiDefaultUpload;
/**
 * 表单提交按钮
 * @type {FormSubmitBtn}
 */
export const SubmitBtn = MaiSubmitBtn;

/**
 * passport行
 */
export const PassportField = MaiPassportField;

/**
 * 表单事件
 */
export class FormEvent {
    /**
     * 检测必填项是否完成
     * @param rule
     * @param form
     * @returns {*}
     */
    static isCompleted(rule, form) {
        for (const key in rule) {
            if (rule[key].required && !form[key]) {
                return MaiFormStatus.EDIT;
            }
        }
        return MaiFormStatus.READY;
    }

    /**
     * 校验全部字段regExp
     * @param rule
     * @param form
     * @returns {*}
     */
    static isValid(rule, form) {
        for (const key in rule) {
            const regExp = rule[key].required ? rule[key].regExp ? rule[key].regExp : /\S+/ : '';
            if (regExp && !regExp.test(form[key] ? form[key] : '')) {
                //错误
                return {
                    result: false,
                    message: rule[key].errorMsg
                }
            }
        }
        //正确
        return {
            result: true,
            message: null
        }
    }

    static picker(options) {
        Picker.init({
            pickerData: options.data || [],
            pickerConfirmBtnText: '确定',
            pickerCancelBtnText: '取消',
            pickerTitleText: options.title || '',
            pickerConfirmBtnColor: [224, 177, 97, 1],
            pickerCancelBtnColor: [153, 153, 153, 1],
            pickerTitleColor: [51, 56, 85, 1],
            pickerToolBarBg: [244, 243, 239, 1],
            pickerBg: [244, 243, 239, 1],
            onPickerConfirm: data => options.handleConfirm(data) || {}
        });
        Picker.show();
    }
}